import googleapiclient.discovery
from getpass import getpass

# 1. Securely capture API Key
api_key = getpass("Enter your YouTube Data API Key: ")
youtube = googleapiclient.discovery.build("youtube", "v3", developerKey=api_key)

search_words = ["de", "la", "que", "el", "en", "y", "a", "los", "del"]

def run_systematic_analysis(iterations=4):
    grand_total_videos = 0
    grand_total_geotagged = 0
    
    # Outer loop for the 4 runs
    for run in range(1, iterations + 1):
        print(f"\n🚀 STARTING RUN {run} OF {iterations}")
        run_videos = 0
        run_geotagged = 0
        
        for word in search_words:
            word_ids = []
            next_page_token = None
            
            # Pagination for 100 videos (2 batches of 50)
            for _ in range(2):
                request = youtube.search().list(
                    part="snippet",
                    q=word,
                    regionCode="MX",
                    type="video",
                    maxResults=50,
                    pageToken=next_page_token
                )
                response = request.execute()
                word_ids.extend([item['id']['videoId'] for item in response['items']])
                next_page_token = response.get('nextPageToken')
                if not next_page_token: break

            # Metadata check for the word
            geotagged_count = 0
            for i in range(0, len(word_ids), 50):
                chunk = word_ids[i:i+50]
                v_request = youtube.videos().list(part="recordingDetails", id=",".join(chunk))
                v_response = v_request.execute()
                for video in v_response.get('items', []):
                    if video.get('recordingDetails', {}).get('location'):
                        geotagged_count += 1
            
            run_videos += len(word_ids)
            run_geotagged += geotagged_count
            
        print(f"✅ Run {run} Complete: {run_videos} videos, {run_geotagged} geotagged.")
        grand_total_videos += run_videos
        grand_total_geotagged += run_geotagged

    # FINAL GRAND TOTAL
    print(f"\n" + "="*30)
    print(f"📊 FINAL GRAND TOTAL (After {iterations} Runs)")
    print(f"Total Videos Analyzed: {grand_total_videos}")
    print(f"Total Geotagged Found: {grand_total_geotagged}")
    if grand_total_videos > 0:
        percent = (grand_total_geotagged / grand_total_videos) * 100
        print(f"Global Geotagging Rate: {percent:.2f}%")
    print("="*30)

if __name__ == "__main__":
    run_systematic_analysis(4)
